/**
 * ajax 封装
 */
layui.define(function (exports) {

    var request = {
        /**
         *
         * @param url 请不要带 ${base!} 变量
         * @param data
         * @returns {*}
         */
        postSyncJSON: function (url, data) {
            var parameter = {}, callback = arguments.length == 3 ? arguments[2] : undefined;
            if (/.*function.*/ig.test(Object.prototype.toString.call(data))) {
                callback = data;
            } else {
                parameter = data;
            }
            this.getAjax(url, parameter, "POST", callback);
        },
        /**
         *
         * @param url 请不要带 ${base!} 变量
         * @param data
         * @returns {*}
         */
        getSyncJSON: function (url, data) {
            var parameter = {}, callback = arguments.length == 3 ? arguments[2] : undefined;
            if (/.*function.*/ig.test(Object.prototype.toString.call(data))) {
                callback = data;
            } else {
                parameter = data;
            }
            this.getAjax(url, parameter, "get", callback);
        },
        ajaxSuccess: function (data, successCallback, failCallback) {
            if (data && data.code == '20000') {
                if (/.*function.*/ig.test(Object.prototype.toString.call(successCallback))) {
                    successCallback.call(this, data);
                }
            } else {
                if (/.*function.*/ig.test(Object.prototype.toString.call(failCallback))) {
                    failCallback.call(this, data);
                }
            }
        },
        getAjax: function (url, parameter, type, callback) {
            var load = 0;
            $.ajax({
                url: base + url,
                type: type || "POST",
                data: parameter || {},
                beforeSend: function () {
                    load = layer.load(0, {shade: 0.2});
                },
                error: function () {
                    layer.close(load);
                },
                dataType: "json",
                success: function (data) {
                    layer.close(load);
                    if (/.*function.*/ig.test(Object.prototype.toString.call(callback))) {
                        callback(data);
                    }
                }
            });
        }
    };

    exports('request', request);

});
